package com.yvan.springmvc;

import com.yvan.Conv;
import com.yvan.platform.Pagin;

import java.util.LinkedHashMap;

/**
 * 用来快速返回结果的model对象
 */
public class ResultModel extends LinkedHashMap<String, Object> {

    public static ResultModel newSuccess() {
        return new ResultModel().success(true);
    }

    public static ResultModel newSuccess(final Object data) {
        return new ResultModel().success(true).putData(data);
    }

    public static ResultModel newFail(final String errorMsg) {
        return new ResultModel().success(false).putMsg(errorMsg);
    }

    public static ResultModel newBootstrapPaginationData(final Pagin pagination, final Object data) {
        return new ResultModel().success(true).putTotal(pagination.getCount()).putRows(data);
    }

    public static Object newEasyuiResult(final long total, final Object rows) {
        return new ResultModel().putTotal(total).putRows(rows);
    }

    public static Object newEasyuiResult(final int total, final Object rows) {
        return new ResultModel().putTotal(total).putRows(rows);
    }

    public ResultModel putMsg(String msg) {
        return set("msg", msg);
    }

    public ResultModel putTitle(String title) {
        return set("title", title);
    }

    public ResultModel putData(Object data) {
        return set("data", data);
    }

    public Object getData() {
        return get("data");
    }

    public ResultModel putTotal(int total) {
        return set("total", total);
    }

    public ResultModel putTotal(long total) {
        return set("total", total);
    }

    public ResultModel putRows(Object rows) {
        return set("rows", rows);
    }

    public ResultModel success(boolean success) {
        return set("success", success);
    }

    public boolean isSuccess() {
        return Conv.NB(get("success"));
    }

    public ResultModel set(String key, Object value) {
        this.put(key, value);
        return this;
    }

    public ResultModel put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
